from PIL import Image
import numpy as np
import pytesseract
import time
import cv2
import re
import os
import math
import operator
from functools import reduce
import cv2 as cv



def zhongma():
    
    os.system("adb devices")
    os.system("adb -s emulator-5554 shell input tap 874 2029")
    time.sleep(2)
    os.system("adb -s emulator-5554 shell input tap 518 2020")
    time.sleep(1)
    xunlainyaun()

def xunlainyaun():

   os.system("adb -s emulator-5554 shell screencap -p sdcard/screen.png") #截取屏幕，图片命名为screen.png
   os.system("adb -s emulator-5554 pull sdcard/screen.png D:\\ppc\\xu.png")
   
   img = Image.open(r"D:\\ppc\xu.png")
   
   cropped = img.crop((815,1990,1038,2076))  # (左，上，右，下 )#截图判断是否存在种马界面
   cropped.save(r"D:\\ppc\xu.png")

   img1_path=r'D:\ppc\xu.png'
   img2_path=r'D:\ppc\shuc\xu.png'
   try:
        # 读取图片
        img1 = cv2.imread(img1_path, cv2.IMREAD_GRAYSCALE)
        img2 = cv2.imread(img2_path, cv2.IMREAD_GRAYSCALE)
 
        # 初始化ORB检测器
        orb = cv2.ORB_create()
        kp1, des1 = orb.detectAndCompute(img1, None)
        kp2, des2 = orb.detectAndCompute(img2, None)
 
        # 提取并计算特征点
        bf = cv2.BFMatcher(cv2.NORM_HAMMING)
 
        # knn筛选结果
        matches = bf.knnMatch(des1, trainDescriptors=des2, k=2)
 
        # 查看最大匹配点数目
        good = [m for (m, n) in matches if m.distance < 0.55 * n.distance]
        #print(len(good))
        #print(len(matches))
        similary = len(good) / len(matches)
        print("两张图片相似度为:%s" % similary)
        if similary==0.9:
            print("等待训练员挑选马中---")
            xunlainyaun()
            time.sleep(1)
        else:
            print("训练员挑选马完毕---")
            quanzd()
        return similary
 
   except:
        print("等待训练员挑选马中---")
        xunlainyaun()
        time.sleep(1)
        return '0'
 
def quanzd():

    print("开始全自动种马")

    os.system("adb -s emulator-5554 shell input tap 741 1712")
    time.sleep(1)
    os.system("adb -s emulator-5554 shell input tap 744 1495")
    time.sleep(1)
    os.system("adb -s emulator-5554 shell input tap 510 2031")
    time.sleep(1)
    os.system("adb -s emulator-5554 shell input tap 761 1622")
    time.sleep(1)
    os.system("adb -s emulator-5554 shell input tap 761 1486")
    time.sleep(1)
    os.system("adb -s emulator-5554 shell input tap 558 2031")
    time.sleep(1)
    os.system("adb -s emulator-5554 shell input tap 851 2026")

    time.sleep(3)
    #跳过剧情
    
    time.sleep(3)
    os.system("adb -s emulator-5554 shell input tap 986 2241")
    time.sleep(2)
    os.system("adb -s emulator-5554 shell input tap 986 2241")
    time.sleep(2)
    os.system("adb -s emulator-5554 shell input tap 524 1565")
    time.sleep(2)
    os.system("adb -s emulator-5554 shell input tap 382 2293")
    time.sleep(2)
    os.system("adb -s emulator-5554 shell input tap 382 2293")
    time.sleep(2)

    saima()

    
def saima():
    #os.system("adb devices")
    #time.sleep(5)
    os.system("adb -s emulator-5554 shell screencap -p sdcard/screen.png") #截取屏幕，图片命名为screen.png
    os.system("adb -s emulator-5554 pull sdcard/screen.png D:\\ppc\\xn.png")
    
    img_path = r"D:\\ppc\xn.png"
    img = cv2.imread(img_path)
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imwrite(r"D:\\ppc\xn.png",gray_image)
    print(img.shape)
    

    #识别速度
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((107,1704,203,1742))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\xn1.png")
    num_1 = Image.open(r'D:\\ppc\xn1.png')
    print("当前速度:"+pytesseract.image_to_string(num_1))

    
    #识别耐力
    img = Image.open(r"D:\\ppc\xn.png")  
    cropped = img.crop((280,1703,373,1742))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\nai.png")
    num_1 = Image.open(r'D:\\ppc\nai.png')
    print("当前耐力:"+pytesseract.image_to_string(num_1))



    #识别爆发
    img = Image.open(r"D:\\ppc\xn.png")    
    cropped = img.crop((445,1702,541,1742))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\bao.png")
    num_1 = Image.open(r'D:\\ppc\bao.png')
    print("当前爆发:"+pytesseract.image_to_string(num_1))
    
    
    #识别根性
    img = Image.open(r"D:\\ppc\xn.png")    
    cropped = img.crop((615,1704,711,1740))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\gen.png")
    num_1 = Image.open(r'D:\\ppc\gen.png')
    print("当前根性:"+pytesseract.image_to_string(num_1))

    #识别学识
    img = Image.open(r"D:\\ppc\xn.png")    
    cropped = img.crop((774,1702,876,1740))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\xue.png")
    num_1 = Image.open(r'D:\\ppc\xue.png')
    print("当前学识:"+pytesseract.image_to_string(num_1))



               
    
    #识别关闭技能页面
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((819,1996,1030,2078))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\jn.png")

    pic1=r"D:\\ppc\jn.png"
    pic2=r"D:\\ppc\shuc\jn.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("关闭技能页面")
        os.system("adb -s emulator-5554 shell input tap 109 2277")
        time.sleep(2)
        saima()
    else:
        
        pass

    


    
    #识别URA比赛
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((638,1981,755,2043))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\URA.png")

    pic1=r"D:\\ppc\URA.png"
    pic2=r"D:\\ppc\shuc\URA.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("URA比赛")
        time.sleep(3)
        os.system("adb -s emulator-5554 shell input tap 748 2017")
        time.sleep(3)
        print("出走")
        os.system("adb -s emulator-5554 shell input tap 758 2030")
        time.sleep(2)
        os.system("adb -s emulator-5554 shell input tap 785 1593")
        time.sleep(10)
        os.system("adb -s emulator-5554 shell input tap 359 2174")
        time.sleep(3)
        os.system("adb -s emulator-5554 shell input tap 359 2174")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 534 2182")
        time.sleep(8)
        os.system("adb -s emulator-5554 shell input tap 796 2204")
        time.sleep(3)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(3)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(5)

    else:
        
        pass



    #识别比赛
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((655,1986,765,2056))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\bis.png")

    pic1=r"D:\\ppc\bis.png"
    pic2=r"D:\\ppc\shuc\bis.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("比赛")
        os.system("adb -s emulator-5554 shell input tap 748 2017")
        time.sleep(8)
        print("出走")
        os.system("adb -s emulator-5554 shell input tap 758 2030")
        time.sleep(8)
        os.system("adb -s emulator-5554 shell input tap 785 1593")
        time.sleep(10)
        tao()
        time.sleep(8)
        os.system("adb -s emulator-5554 shell input tap 359 2174")       
        time.sleep(7)
        os.system("adb -s emulator-5554 shell input tap 359 2174")
        time.sleep(7)
        os.system("adb -s emulator-5554 shell input tap 534 2182")
        time.sleep(7)
        os.system("adb -s emulator-5554 shell input tap 796 2204")
        time.sleep(7)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(7)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(7)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(7)

    else:
        
        pass

    #识别心情 绝好调
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((874,227,941,252))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\xq.png")

    pic1=r"D:\\ppc\xq.png"
    pic2=r"D:\\ppc\shuc\xq.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr<=a:
        print("当前心情 绝好调")
    else:
        pass

    
    #识别完成培养
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((13,13,214,45))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\cg.png")

    pic1=r"D:\\ppc\cg.png"
    pic2=r"D:\\ppc\shuc\cg.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        cg()
        
    else:
        pass


    #养育失败 取消
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((107,1547,494,1625))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\quxiao.png")

    pic1=r"D:\\ppc\quxiao.png"
    pic2=r"D:\\ppc\shuc\quxiao.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("养育失败 取消")
        os.system("adb -s emulator-5554 shell input tap 259 1573")
        time.sleep(2)
    else:
        pass
    #识别心情 好调
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((912,230,975,253))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\haodiao.png")

    pic1=r"D:\\ppc\haodiao.png"
    pic2=r"D:\\ppc\shuc\haodiao.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("当前心情 好调")
        os.system("adb -s emulator-5554 shell input tap 532 2106")
        time.sleep(2)
    else:
        pass

    
    #识别心情 不调
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((874,227,941,252))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\butiao.png")

    pic1=r"D:\\ppc\butiao.png"
    pic2=r"D:\\ppc\shuc\butiao.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("当前心情 不调")
        os.system("adb -s emulator-5554 shell input tap 532 2106")
        time.sleep(2)
    else:
        pass


        
    #识别心情 绝不调
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((874,227,941,252))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\juebutiao.png")

    pic1=r"D:\\ppc\juebutiao.png"
    pic2=r"D:\\ppc\shuc\juebutiao.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("当前心情 绝不调")
        os.system("adb -s emulator-5554 shell input tap 532 2106")
        time.sleep(2)
    else:

        pass

            
    #识别心情 普通
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((874,227,941,252))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\putong.png")

    pic1=r"D:\\ppc\putong.png"
    pic2=r"D:\\ppc\shuc\putong.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("当前心情 普通")
        os.system("adb -s emulator-5554 shell input tap 532 2106")
    else:
        pass



        

        
    #识别粉丝
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((438,979,785,1263))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\fens.png")

    pic1=r"D:\\ppc\fens.png"
    pic2=r"D:\\ppc\shuc\fens.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("当前粉丝不够 需要比赛")
        os.system("adb -s emulator-5554 shell input tap 756 1577")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 758 2030")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 785 1593")
        time.sleep(10)
        os.system("adb -s emulator-5554 shell input tap 359 2174")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 359 2174")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 534 2182")
        time.sleep(8)
        os.system("adb -s emulator-5554 shell input tap 796 2204")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 497 2096")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 513 2083")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 521 2090")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 521 2090")
    else:
        
        pass
            
    #识别支线对话
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((47,1239,1027,1330))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\zhix.png")

    pic1=r"D:\\ppc\zhix.png"
    pic2=r"D:\\ppc\shuc\zhix.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("60364080275441951")
    if nr==a:
        print("支线对话")
        os.system("adb -s emulator-5554 shell input tap 502 1275")
    else:
        
        pass

       
    
    #识别支线对话
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((51,1250,100,1316))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\zhixx.png")

    pic1=r"D:\\ppc\zhixx.png"
    pic2=r"D:\\ppc\shuc\zhixx.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("支线对话")
        os.system("adb -s emulator-5554 shell input tap 502 1275")
        time.sleep(5)
    else:
        
        pass

           
    
    #识别支线对话
    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((51,1089,108,1147))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\zhixxx.png")

    pic1=r"D:\\ppc\zhixxx.png"
    pic2=r"D:\\ppc\shuc\zhixxx.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("支线对话")
        os.system("adb -s emulator-5554 shell input tap 502 1275")
        time.sleep(5)
    else:
        
        pass



    #识别 保健室"
    #os.system("adb -s emulator-5554 shell screencap -p sdcard/screen.png") #截取屏幕，图片命名为screen.png
    #os.system("adb -s emulator-5554 pull sdcard/screen.png D:\\ppc\\boajians.png")

    img = Image.open(r"D:\\ppc\xn.png")   
    cropped = img.crop((159,2055,333,2177))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\boajians.png")

    pic1=r"D:\\ppc\boajians.png"
    pic2=r"D:\\ppc\shuc\boajians.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("")
    else:
        print("")
        
        pass
    saimaxunlain()


def tao():
       

    os.system("adb -s emulator-5554 shell screencap -p sdcard/screen.png") #截取屏幕，图片命名为screen.png
    os.system("adb -s emulator-5554 pull sdcard/screen.png D:\\ppc\\tao.png")

    img = Image.open(r"D:\\ppc\tao.png")   
    cropped = img.crop((971,1221,1006,1240))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\tao.png")

    pic1=r"D:\\ppc\tao.png"
    pic2=r"D:\\ppc\shuc\tao.png"
    image1 = Image.open(pic1)
    image2 = Image.open(pic2)
    histogram1 = image1.histogram()
    histogram2 = image2.histogram()
    differ = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2,histogram1, histogram2)))/len(histogram1))
    nr=str(differ)  
    nr=str(nr)
    nr=nr.replace(r".0","")
    nr=nr.replace(r"\n","")
    nr=nr.replace(r".","")
    nr=str(nr)  
    #print(nr)
    nr+=str(1)
    #print(nr)
    a=("01")
    if nr==a:
        print("当前选项是“大逃”")

    else:
        print("当前选项不是“大逃”")
        os.system("adb -s emulator-5554 shell input tap 859 1325")
        time.sleep(1)
        os.system("adb -s emulator-5554 shell input tap 889 1383")
        time.sleep(2)
        os.system("adb -s emulator-5554 shell input tap 775 1581")

        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 359 2174")       
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 359 2174")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 534 2182")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 780 2197")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 513 2083")
        time.sleep(2)
        os.system("adb -s emulator-5554 shell input tap 513 2083")
        time.sleep(5)
        os.system("adb -s emulator-5554 shell input tap 513 2083")
        time.sleep(5)
        saima()
        pass

def saimaxunlain():

    time.sleep(3)
    os.system("adb -s emulator-5554 shell input tap 536 1895")
    time.sleep(2)

    os.system("adb -s emulator-5554 shell screencap -p sdcard/screen.png") #截取屏幕，图片命名为screen.png
    os.system("adb -s emulator-5554 pull sdcard/screen.png D:\\ppc\\xu.png")
    #识别体力条
    img_path = r"D:\\ppc\xu.png"
    img = cv2.imread(img_path)
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imwrite(r"D:\\ppc\xu.png",gray_image)
    print(img.shape)
    
    img = Image.open(r"D:\\ppc\xu.png")   
    cropped = img.crop((160,1796,260,1839))  # (左，上，右，下 )
    cropped.save(r"D:\\ppc\tili.png")
    num_1 = Image.open(r'D:\\ppc\tili.png')
    an=(pytesseract.image_to_string(num_1))
    an=str(an)
    an=an.replace(r"%","")
    an=an.replace(r"\n","")
    a=str(20)
    print(an)
    if an>a:
        print("回复体力")
        #os.system("adb -s emulator-5554 shell input tap 123 2260")
        #time.sleep(1)
        os.system("adb -s emulator-5554 shell input tap 123 2260")
        time.sleep(1)
        os.system("adb -s emulator-5554 shell input tap 147 1970")
        time.sleep(10)
        saima()
    else:
        
       time.sleep(1)
       os.system("adb -s emulator-5554 shell input tap 147 1970")
       time.sleep(10)
       saima()
       pass

def cg():

    print("恭喜！！ 已经顺利完成培养")
    a=input("")


#zhongma()
saima()
#tao()